package com.ruoyi.operation.monitor.Impl;

import cn.hutool.extra.ftp.Ftp;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.operation.domain.TFileResult;
import com.ruoyi.operation.domain.vo.FileMonitorVo;
import com.ruoyi.operation.monitor.IFileMonitorService;
import com.ruoyi.operation.monitor.IMonitorStrategy;
import com.ruoyi.operation.service.ITFileGroupService;
import com.ruoyi.operation.service.ITFileListService;
import com.ruoyi.operation.service.ITFileResultService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author Zzneko
 * @Date 2025/7/22 10:50
 * @Descprition
 **/
@Service
@RequiredArgsConstructor
public class IFileMonitorServiceImpl implements IFileMonitorService {

    private final ITFileGroupService itFileGroupService;
    private final ITFileListService itFileListService;
    private final ITFileResultService itFileResultService;
    private final Map<String, IMonitorStrategy> iMonitorStrategyMap;
    @Override
    public R<Void> checkFileExist(String groupId) {
        Map<String, List<FileMonitorVo>> divideByTestMode = itFileGroupService.selectAllMonitorList(groupId).stream().collect(Collectors.groupingBy(FileMonitorVo::getTestmode));
        List<TFileResult> arrayList = new ArrayList<>();
        //分模式处理
        divideByTestMode.forEach((mode,value)->{
            List<TFileResult> tFileResults = iMonitorStrategyMap.get(mode).handleMonitorResult(value);
            arrayList.addAll(tFileResults);
        });
        itFileResultService.saveBatch(arrayList);
        return R.ok();
    }
}
